package tt.dz.biz.service.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import javax.annotation.Resource;
import org.apache.avro.Protocol;
import org.apache.avro.Protocol.Message;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tt.dz.biz.service.user.OperListService;
import tt.dz.common.DateUtil;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.CommonErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.RPCUtil;
import tt.dz.vo.OperListVO;

@Service("rpcQueryOperListService")
public class RPCQueryOperListService
  implements RPCBaseService
{
  @Resource(name="operListService")
  private OperListService operListService;
  
  public Object execute(Protocol.Message message, Object request, Protocol protocol)
  {
    String name = message.getName();
    Object reqJson = ((GenericRecord)request).get("req");
    String resJson = "";
    if ("queryOperListBySys".equals(name)) {
      resJson = queryOperListBySys(reqJson.toString());
    }
    return resJson;
  }
  
  public String queryOperListBySys(String reqJson)
  {
    JSONObject resObj = null;
    try
    {
      JSONObject reqObj = JSON.parseObject(reqJson);
      String sysCode = reqObj.getString("sysCode");
      if (StringUtils.isBlank(sysCode))
      {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.PARAM_FORMAT_ERROR.value(), "必传参数有缺失！");
        
        return JSON.toJSONString(resObj);
      }
      String startTime = reqObj.getString("startTime");
      String endTime = reqObj.getString("endTime");
      if ((StringUtils.isNotBlank(startTime)) && (!DateUtil.validateIsDate("yyyy-MM-dd", startTime)))
      {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.PARAM_FORMAT_ERROR.value(), "startTime传入格式错误！");
        
        return JSON.toJSONString(resObj);
      }
      if ((StringUtils.isNotBlank(endTime)) && (!DateUtil.validateIsDate("yyyy-MM-dd", endTime)))
      {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.PARAM_FORMAT_ERROR.value(), "endTime传入格式错误！");
        
        return JSON.toJSONString(resObj);
      }
      List<OperListVO> operListList = this.operListService.queryOperListBySys(sysCode, startTime, endTime);
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(1), null, null);
      resObj.put("result", JSON.toJSONString(operListList));
    }
    catch (JSONException e)
    {
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.FORMAT_ERROR.value(), DzErrorCode.CommonErrorCode.FORMAT_ERROR
        .getDesc());
    }
    catch (Exception e)
    {
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), "角色列表异常,请稍后重试！");
    }
    return JSON.toJSONString(resObj);
  }
}
